<?php
	
	namespace app\api\controller;
	
	use app\common\model\GrabConfig;
	use Exception;
	use think\Db;
	use app\common\controller\Api;
	
	class Grab extends Api
	{
		
		protected $noNeedLogin = [];
		protected $noNeedRight = ['*'];
		
		/**
		 * @Author   : crafters@qq.com
		 * @DateTime : 2022-3-6 11:07
		 * @Introduce: 抓取配置
		 * @throws \think\db\exception\DataNotFoundException
		 * @throws \think\db\exception\ModelNotFoundException
		 * @throws \think\exception\DbException
		 */
		public function Index()
		{
			
			if (!$this->request->isGet()) {
				$this->error('请求方法不允许', null, 405);
			}
			
			$config = Db::name('grab_config')
                ->field('sensitive_words_cate,website_area')
                ->where(['user_id' => $this->auth->id])->find();
			
			$this->success('成功', $config);
			
		}
		
		/**
		 * @Author   : crafters@qq.com
		 * @DateTime : 2022-3-6 10:51
		 * @Introduce: 抓取配置
		 */
		public function Config()
		{
			$sensitive_words_cate = $this->request->param('sensitive_words_cate');
			$website_area         = $this->request->param('website_area');
			
			if (!$sensitive_words_cate){
				$this->error('敏感词分类id不能为空');
			}
			
			if (!$website_area){
				$this->error('网址地区id不能为空');
			}
			
			$data = [
				'sensitive_words_cate' => $sensitive_words_cate,
				'website_area'         => $website_area,
                'user_id'              => $this->auth->id,
			];
			
			$grabConfig = new GrabConfig();
            $grabdata = $grabConfig::get(['user_id'=>$this->auth->id]);
			if(empty($grabdata)){
                $grabConfig->save($data);
            }else{
                $grabdata->save($data, ['user_id' => $this->auth->id]);
            }

			$this->success('更新成功');
		}
	}
